package dao;

import model.Patient;
import util.DbUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

public class PatientDao {
    public static void insert(Patient model)
    {
        try
        {
            Connection conn= DbUtil.getConnection();
            String sql="insert into patient(id,name,sex,age,password)";
            sql +="values(?,?,?,?,?);";
            PreparedStatement pstmt=conn.prepareStatement(sql);
            pstmt.setInt(1, model.getId());
            pstmt.setString(2, model.getName());
            pstmt.setInt(3, model.getSex());
            pstmt.setInt(4, model.getAge());
            pstmt.setString(5, model.getPassword());
            pstmt.executeUpdate();
            DbUtil.release(pstmt, conn);
        } catch (ClassNotFoundException | SQLException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void update(Patient model)
    {
        try
        {
            Connection conn=DbUtil.getConnection();
            String sql="update patient set name = ?, sex = ?, age = ?, password=? ";
            sql +="where id = ?;";
            PreparedStatement pstmt=conn.prepareStatement(sql);
            pstmt.setString(1, model.getName());
            pstmt.setInt(2, model.getSex());
            pstmt.setInt(3, model.getAge());
            pstmt.setString(4, model.getPassword());
            pstmt.setInt(5, model.getId());
            pstmt.executeUpdate();
            DbUtil.release(pstmt, conn);
        } catch (ClassNotFoundException | SQLException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void delete(Integer id)
    {
        try
        {
            Connection conn=DbUtil.getConnection();
            String sql="delete from patient where id = ?;";
            PreparedStatement pstmt=conn.prepareStatement(sql);
            pstmt.setInt(1, id);
            pstmt.executeUpdate();
            DbUtil.release(pstmt, conn);
        } catch (ClassNotFoundException | SQLException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static List<Patient> select(Integer id, String name, Integer sex, Integer age, String password)
    {
        try
        {
            String sql="select id, name, sex, age, password from patient ";
            sql += "where 1=1 ";
            if(id != null)
            {
                sql += "and id = '"+id+"' ";
            }
            if(name != null)
            {
                sql += "and name = '"+name+"' ";
            }
            if(sex != null)
            {
                sql += "and sex = '"+sex+"' ";
            }
            if(age != null)
            {
                sql += "and age = '"+age+"' ";
            }
            if(password != null)
            {
                sql += "and password = '"+password+"' ";
            }
            sql +=";";
            return fillResult(sql);
        } catch (ClassNotFoundException | SQLException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return new LinkedList<>();
        }
    }

    private static List<Patient> fillResult(String sql) throws ClassNotFoundException, SQLException
    {
        List<Patient> patientList = new LinkedList<>();
        Connection conn = DbUtil.getConnection();
        PreparedStatement pstmt=conn.prepareStatement(sql);
        ResultSet rs= pstmt.executeQuery();
        while(rs.next())
        {
            Patient patient = new Patient(rs.getInt("id"),rs.getString("name"),rs.getInt("sex"),rs.getInt("age"),rs.getString("password"));
            patientList.add(patient);
        }
        DbUtil.release(pstmt, conn);
        return patientList;
    }

}
